package org.hiucung.orangeppjoke.ui.detail;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelStore;
import androidx.paging.ItemKeyedDataSource;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.hiucung.libcommon.view.EmptyView;
import org.hiucung.orangeppjoke.R;
import org.hiucung.orangeppjoke.databinding.LayoutFeedDetailBottomInateractionBinding;
import org.hiucung.orangeppjoke.model.Comment;
import org.hiucung.orangeppjoke.model.Feed;
import org.hiucung.orangeppjoke.ui.MutableItemKeyedDataSource;

/**
 * @author : its-kicc
 * @date : 2021/10/8 14:58
 */
public abstract class ViewHandler {
    private final FeedDetailViewModel viewModel;

    protected FragmentActivity mActivity;
    protected Feed mFeed;
    protected RecyclerView mRecyclerView;

    protected FeedCommentAdapter feedCommentAdapter;

    protected LayoutFeedDetailBottomInateractionBinding mInateractionBinding;
    private CommentDialog mCommentDialog;

    public ViewHandler(FragmentActivity activity){
        mActivity = activity;
        viewModel = ViewModelProviders.of(activity).get(FeedDetailViewModel.class);

    }


    @CallSuper
    public void bindInitData(Feed feed){
        mInateractionBinding.setOwner(mActivity);
        mFeed = feed;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL,false));

        feedCommentAdapter = new FeedCommentAdapter(mActivity);
        mRecyclerView.setItemAnimator(null);
        mRecyclerView.setAdapter(feedCommentAdapter);

        viewModel.setItemId(mFeed.itemId);
        viewModel.getPageData().observe(mActivity, new Observer<PagedList<Comment>>() {
            @Override
            public void onChanged(PagedList<Comment> comments) {
                feedCommentAdapter.submitList(comments);
                handleEmpty(comments.size() > 0);
            }
        });
        //点击假的输入框之后，对话框弹起
        mInateractionBinding.inputView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCommentDialog == null){
                    mCommentDialog = CommentDialog.newInstance(mFeed.itemId);
                }
                mCommentDialog.setCommentAddListener(new CommentDialog.commentAddListener() {
                    @Override
                    public void onAddComment(Comment comment) {
                        MutableItemKeyedDataSource<Integer,Comment> mutableItemKeyedDataSource = new MutableItemKeyedDataSource<Integer, Comment>((ItemKeyedDataSource) viewModel.getDataSource()) {
                            @NonNull
                            @Override
                            public Integer getKey(@NonNull Comment item) {
                                return item.id;
                            }
                        };
                        mutableItemKeyedDataSource.data.add(comment);
                        mutableItemKeyedDataSource.data.addAll(feedCommentAdapter.getCurrentList());
                        PagedList<Comment> comments = mutableItemKeyedDataSource.buildNewPagedList(feedCommentAdapter.getCurrentList().getConfig());
                        feedCommentAdapter.submitList(comments);
                    }
                });

                mCommentDialog.show(mActivity.getSupportFragmentManager(),"comment_dialog");
            }
        });
    }

    private EmptyView mEmptyView;

    private void handleEmpty(boolean hasData){
        if (hasData){
            if ((mEmptyView!=null)) {
                feedCommentAdapter.removeHeaderView(mEmptyView);
            }
        }else {
            if (mEmptyView == null){
                mEmptyView = new EmptyView(mActivity);
                mEmptyView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                mEmptyView.setTitle(mActivity.getString(R.string.feed_comment_empty));
                feedCommentAdapter.addHeaderView(mEmptyView);
            }
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mCommentDialog != null && mCommentDialog.isAdded()){
            mCommentDialog.onActivityResult(requestCode,resultCode,data);
        }
    }

    public void onPause() {

    }

    public void onResume() {

    }

    public void onBackPressed() {

    }

}
